import { BCGameLog } from "../../utils/BCGameLog";

export interface HTTP_RESPONSE {
	[name: string]: string
}

export interface BCHttpMsgType {
    'status': boolean,						//是否请求成功
	'code'?: number,						//错误码
    'data'?: any,							//返回的数据
    'msg'?: string							//提示文字
}

export class BCHttpCommon {
	static httpPost(url: string, postData: Object, headMap?: Map<string, any>): Promise<BCHttpMsgType>{
		BCGameLog.log("[HttpCommon-Post]", url);
		return new Promise(res=>{
			let http = new XMLHttpRequest();
			http.onreadystatechange = () => {
				if (http.readyState != 4) {
					return;
				}
				const response: string = http.responseText;
				if(!response){
					// res({ status: false, msg: "Lütfen tekrar deneyin." });
					return;
				}
				BCGameLog.log("[HttpCommon-Post]", response);
				const responseData = JSON.parse(response);
				if (responseData.code == 200) {
					res({ status: true, code: responseData.code, data: responseData.data, msg: responseData.msg });
				} else {
					res({ status: false, code: responseData.code, msg: responseData.msg });
				}
			};

			http.ontimeout = () => {
				BCGameLog.error("[HttpCommon-Post]", "http请求超时:" + url);
				res({ status: false, msg: "Zaman aşımı" });
			};

			http.onerror = () => {
				BCGameLog.error("[HttpCommon-Post]", "http请求出错:" + url);
				res({ status: false, msg: "Lütfen tekrar deneyin." });
			};
			http.open('POST', url, true);
			http.setRequestHeader("Content-Type", "application/json");
			headMap && headMap.forEach((value: any, key: string) => {
				http.setRequestHeader(key, value); // 可以定义请求头带给后端
			});
			http.timeout = 5000;
			http.send(JSON.stringify(postData));
		});
	}

    static httpGet(url: string, headMap?: Map<string, any>): Promise<BCHttpMsgType>{
		BCGameLog.log("[HttpCommon-Get]", url);
		return new Promise(res=>{
			let http: XMLHttpRequest = new XMLHttpRequest();
			http.onreadystatechange = function () {
				if (http.readyState != 4) {
					return;
				}
				const response: string = http.responseText;
				if (!response) {
					// res({ status: false, msg: "Lütfen tekrar deneyin." });
					return;
				}
				BCGameLog.log("[HttpCommon-Get]", response);
				const responseData = JSON.parse(response);
				if (responseData.code == 200) {
					res({ status: true, code: responseData.code, data: responseData.data, msg: responseData.msg });
				} else {
					res({ status: false, code: responseData.code, msg: responseData.msg });
				}
			};
			http.ontimeout = () => {
				BCGameLog.error("[HttpCommon-Get]", "http请求超时:" + url);
				res({ status: false, msg: "Zaman aşımı" });
			};

			http.onerror = () => {
				BCGameLog.error("[HttpCommon-Get]", "http请求出错:" + url);
				res({ status: false, msg: "Lütfen tekrar deneyin." });
			};
			http.open("GET", url, true);
			http.setRequestHeader("Content-Type", "application/json");
			headMap && headMap.forEach((value: any, key: string)=>{
				http.setRequestHeader(key, value); // 可以定义请求头带给后端
			});
			http.timeout = 5000;
			http.send();
		});
	}
}
